﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_region : System.Web.UI.Page
{
    // Fields

    protected Region rg = new Region();
    protected RegionManager rm = new RegionManager();


    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.rg.RegionName = this.txtRegionName.Value;
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.rg.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                DataTable regionById = this.rm.GetRegionById(this.rg.Id);
                this.rg.ParentRegion = Convert.ToInt32(regionById.Rows[0]["parentRegion"]);
                if (this.rm.ModifyRegion(this.rg))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"region_list.aspx?pid=" + this.rg.ParentRegion + "\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
        }
    }

    public StringBuilder GetBcakLocation()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["id"] != null)
            {
                DataTable regionById = this.rm.GetRegionById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (regionById.Rows.Count > 0)
                {
                    builder.Append("location.href='region_list.aspx?pid=" + regionById.Rows[0]["parentRegion"].ToString() + "';");
                }
                return builder;
            }
            if (base.Request.QueryString["pid"] != null)
            {
                builder.Append("location.href='region_list.aspx?pid=" + base.Request.QueryString["pid"] + "';");
            }
        }
        catch
        {
        }
        return builder;
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑地区");
                return builder;
            }
            if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["pid"] != null))
            {
                builder.Append("添加地区");
            }
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack)
        {
            if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
            {
                this.RegionBind();
            }
            else if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["pid"] != null))
            {
                if (Convert.ToInt32(base.Request.QueryString["pid"]) == 0)
                {
                    this.lblParentRegion.InnerText = "顶级地区";
                }
                else
                {
                    DataTable regionById = this.rm.GetRegionById(Convert.ToInt32(base.Request.QueryString["pid"]));
                    if (regionById.Rows.Count > 0)
                    {
                        this.lblParentRegion.InnerText = regionById.Rows[0]["regionName"].ToString();
                    }
                }
            }
        }
    }

    private void RegionBind()
    {
        try
        {
            DataTable regionById = this.rm.GetRegionById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (regionById.Rows.Count > 0)
            {
                if (Convert.ToInt32(regionById.Rows[0]["parentRegion"]) != 0)
                {
                    DataTable table2 = this.rm.GetRegionById(Convert.ToInt32(regionById.Rows[0]["parentRegion"]));
                    if (table2.Rows.Count > 0)
                    {
                        this.lblParentRegion.InnerText = table2.Rows[0]["regionName"].ToString();
                    }
                }
                else
                {
                    this.lblParentRegion.InnerText = "顶级地区";
                }
                this.txtRegionName.Value = regionById.Rows[0]["regionName"].ToString();
            }
        }
        catch
        {
        }
    }

}